﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using BO.Infrastructure.StaticData;
using GUI.Extensions;
using BO.News;
using System.Text;
using BO.Infrastructure.Utils;

namespace GUI.Controls.News
{
    public partial class MostViewNews : System.Web.UI.UserControl
    {
        private const string Format = "<li>{3}"
                                      + "<div class=\"scope clearfix\">"
                                      + "<h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2>" +
                                      "{4}"
                                      + "</div>"
                                      + "</li>";

        private const string PublishedDateFormat = "<p class=\"prop_publishinfo\">"
                                                   + "<b><span>{0}</span></b>"
                                                   + "</p>";

        private const int Top = 9;
        private const int ImageWidth = 100;
        private const int ImageHeight = 75;

        public bool DisplayPublishedDate { get; set; }
        public bool GetDataFromNoCacheLayer = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            var bo = CacheSettingExtensions.EnableCacheHtml && GetDataFromNoCacheLayer ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var dt = bo.GetCareableNews(18); //fix tạm 18 bài sau đó lọc, vì lấy dữ liệu bên anh Hưng về bị trùng.
            Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                var lstKey = new List<long>();
                foreach (DataRow r in dt.Rows)
                {
                    if (lstKey.Count < Top && !lstKey.Contains((long)r[SqlConstants.NewsPublish.Id]))
                    {
                        var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight,lazyLoad:true);
                        sb.AppendFormat(Format,
                                        r[SqlConstants.NewsPublish.Url],
                                        r[SqlConstants.NewsPublish.Title],
                                        r[SqlConstants.NewsPublish.EncodedTitle],
                                        avatar,
                                        DisplayPublishedDate
                                            ? BuildDistributionDate(
                                                PresentationUtils.Object2DateTime(
                                                    r[SqlConstants.NewsPublish.DistributionDate].ToString()))
                                            : string.Empty);
                        lstKey.Add((long)r[SqlConstants.NewsPublish.Id]);
                    }
                }
                ltrData.Text = sb.ToString();
                return true;
            }
            return false;
        }

        private string BuildDistributionDate(DateTime dateTime)
        {
            return string.Format(PublishedDateFormat, dateTime.ToString("dd/MM/yyyy HH:mm"));
        }
    }
}